package com.ruoyi.gateway.handler;

import com.ruoyi.gateway.config.SwaggerProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import org.springdoc.core.models.GroupedOpenApi;
import org.springdoc.core.properties.SwaggerUiConfigProperties;

import java.util.List;

@RestController
@RequestMapping("/swagger-resources")
public class SwaggerHandler {

    private final SwaggerProvider swaggerProvider;
    private final SwaggerUiConfigProperties swaggerUiConfigProperties;

    @Autowired
    public SwaggerHandler(SwaggerProvider swaggerProvider,
                          SwaggerUiConfigProperties swaggerUiConfigProperties) {
        this.swaggerProvider = swaggerProvider;
        this.swaggerUiConfigProperties = swaggerUiConfigProperties;
    }

    @GetMapping("/configuration/ui")
    public Mono<ResponseEntity<SwaggerUiConfigProperties>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(swaggerUiConfigProperties, HttpStatus.OK));
    }

    @GetMapping("")
    public Mono<ResponseEntity<List<GroupedOpenApi>>> swaggerResources() {
        return Mono.just(new ResponseEntity<>(swaggerProvider.getApiGroups(), HttpStatus.OK));
    }
}